package dm_pm;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;

public class IO {
	public static Vector<String[]> readFromCSV(String path) throws Exception{
		BufferedReader bufRdr = new BufferedReader(new FileReader(new File(path)));
		Vector<String[]> contents = new Vector<String []>();
		String line;
		while((line = bufRdr.readLine()) != null){
			StringTokenizer st = new StringTokenizer(line,",");
			int numTokens = st.countTokens();
			String[] tokens = new String[numTokens];
			for(int i=0; i<numTokens; i++){
				tokens[i] = st.nextToken().trim();
			}	
			contents.add(tokens);
		}
		bufRdr.close();
		return contents;
	}
	public static void writeToCSV(String path, String[][] content) throws Exception{
		BufferedWriter bufWr = new BufferedWriter(new FileWriter(new File(path)));
		
		for(int i=0; i<content.length; i++){
			for(int j=0; j<content[i].length; j++){
				bufWr.write(content[i][j].trim());
				if(j+1 != content[i].length){
					bufWr.write(',');
				}
			}
			if(i+1 != content.length){
				bufWr.newLine();
			}
		}
		bufWr.close();
	}
	public static void logError(String msg){
		System.err.println("Error: "+msg);
	}
	public static void logWarning(String msg){
		System.out.println("Warning: "+msg);
	}
	public static void logInfo(String msg){
		System.out.println("Info: "+msg);
	}
}
